<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model;

/**
 * 用户模型
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class UserModel extends Model {

    protected $_validate = array(
        array('nickname', '1,16', '昵称长度为1-16个字符', self::EXISTS_VALIDATE, 'length'),
        array('nickname', '', '昵称被占用', self::EXISTS_VALIDATE, 'unique'), //用户名被占用
    );
    

    public function lists($status = 1, $order = 'uid DESC', $field = true){
        $map = array('status' => $status);
        return $this->field($field)->where($map)->order($order)->select();
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){
        //注销session
        session('user_info', null);
        //注销cookie
        cookie('user_info', null);
    }

    /**
     * 获取用户信息
     * @author  王中艺
     * @param   $userId
     * @return  $userInfo
     */
    function getUserInfo($userId){
        $info   = $this->where("uid = %d", $userId)->find();
        return $info;
    }

    /**
     * 用户注册
     * @param   $username
     * @param   $password
     * @param   $email
     * @return  $userInfo
     */
    function register($username, $password, $email){
        $isExt  = $this->where('username = "'.$username.'"')->count();
        if($isExt > 0)
            return C('ERROR_CODE.114');

        $userInfo   = array(
            'username'  => $username,
            'nickname'  => $username,
            'password'  => $password,
            'email' => $email,
            'reg_ip'    => get_client_ip(1),
            'reg_time'  => time(),
        );

        $uid    = $this->add($userInfo);
        if(!$uid)
            return C('ERROR_CODE.116');

        $userInfo['uid']    = $uid;
        return $userInfo;
    }

    /**
     * 用户登录
     * @param   $username
     * @param   $password
     * @return  array   $userInfo
     */
    function login($username, $password){
        $userInfo   = $this->where('username = "'.$username.'"')->find();
        if(!$userInfo)  
            return C('ERROR_CODE.110');

        if($userInfo['password'] != $password)
            return C('ERROR_CODE.112');

        if($userInfo['status'] != 1)
            return C('ERROR_CODE.118');

        //信息更改
        $userInfo['last_login_ip']  = get_client_ip(1);
        $userInfo['last_login_time']    = time();
        $userInfo['login']++;
        $userInfo   = $this->update($userInfo);

        //注册session
        session('user_info', $userInfo);

        return $userInfo;
    }

    /**/
    function saveRemember($uid,$identifier,$token,$timeout){
        $userInfo['identifier'] = $identifier;
        $userInfo['token'] = $token;
        $userInfo['timeout'] = $timeout;
        $ret    = $this->where('uid='.$uid)->data($userInfo)->save();
        if(!$ret)
            return '信息更改失败!';
//        session('user', $userInfo);
        return true;

    }

    //验证用户是否永久登录（记住我）
    public function checkRemember(){
        $arr = array();
        $now = time();

        list($identifier,$token) = explode(':',$_COOKIE['auth']);
        if (ctype_alnum($identifier) && ctype_alnum($token)){
            $arr['identifier'] = $identifier;
            $arr['token'] = $token;
        }else{
            return false;
        }
        $map['identifier'] = $arr['identifier'];
        $info = $this->where($map)->find();
        if($info){
            if($arr['token'] != $info['token']){
                return false;
            }else if($now > $info['timeout']){
                return false;
            }else{
                return $info;
            }
        }else{
            return false;
        }
    }
    /*****************************************/





    /**
     * 更新用户信息
     * @param   array   $userInfo(uid必有)
     * @return  $userInfo
     */
    function update($userInfo){
        $ret    = $this->data($userInfo)->save();
        if(!$ret)
            return '信息更改失败!';
        session('user', $userInfo);
        return $userInfo;
    }
    /**
     * 管理员更改用户密码
     * @param   $userId
     *
     * @param   $newPass
     * @return  boolean
     */
    function modifyPwd($userId, $newPass){
        $userInfo   = $this->getUserInfo($userId);
        if(!$userInfo)
            return '用户未找到';


        $userInfo['password']   = $newPass;
        $status     = $this->save($userInfo);
        if($status)
            return true;
        return '密码修改失败!';
    }
}
